local leiji = fk.CreateSkill {

  name = "joyex__leiji",

  tags = { Skill.Compulsory, },

}



leiji:addEffect(fk.CardUsing, {
  name = "joyex__leiji",
  anim_type = "offensive",
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(leiji.name) and target == player and (data.card.name == "jink" or data.card.trueName == "lightning")
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), function (p)
      return p.id end), 1, 1, "#joyex__leiji-choose", leiji.name, true)
    if #to > 0 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tar = room:getPlayerById(event:getCostData(self))
    local judge = {
      who = tar,
      reason = leiji.name,
      pattern = ".|.|spade,club",
    }
    room:judge(judge)
    if judge.card.suit == Card.Spade then
      room:damage{
        from = player,
        to = tar,
        damage = 2,
        damageType = fk.ThunderDamage,
        skillName = leiji.name,
      }
    elseif judge.card.suit == Card.Club then
        if player:isWounded() then
          room:recover({
            who = player,
            num = 1,
            recoverBy = player,
            skillName = leiji.name,
          })
        end
        if player.dead then return false end
        room:damage{
          from = player,
          to = tar,
          damage = 1,
          damageType = fk.ThunderDamage,
          skillName = leiji.name,
        }
    end
  end,
})
leiji:addEffect(fk.CardResponding, {
  name = "joyex__leiji",
  anim_type = "offensive",
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(leiji.name) and target == player and (data.card.name == "jink" or data.card.trueName == "lightning")
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(player, table.map(player.room:getOtherPlayers(player), function (p)
      return p.id end), 1, 1, "#joyex__leiji-choose", leiji.name, true)
    if #to > 0 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tar = room:getPlayerById(event:getCostData(self))
    local judge = {
      who = tar,
      reason = leiji.name,
      pattern = ".|.|spade,club",
    }
    room:judge(judge)
    if judge.card.suit == Card.Spade then
      room:damage{
        from = player,
        to = tar,
        damage = 2,
        damageType = fk.ThunderDamage,
        skillName = leiji.name,
      }
    elseif judge.card.suit == Card.Club then
        if player:isWounded() then
          room:recover({
            who = player,
            num = 1,
            recoverBy = player,
            skillName = leiji.name,
          })
        end
        if player.dead then return false end
        room:damage{
          from = player,
          to = tar,
          damage = 1,
          damageType = fk.ThunderDamage,
          skillName = leiji.name,
        }
    end
  end,
})

leiji:addEffect("maxcards", {
  name = "#joyex__leiji_maxcards",
  frequency = Skill.Compulsory,
  correct_func = function(self, player)
     if player:hasSkill("joyex__leiji") then
      return 2
    end
  end,
})

return leiji